package com.easybuilder.core.rbac.service.impl;

import com.easybuilder.common.base.entity.PageQueryWrapper;
import com.easybuilder.common.base.entity.PageResultWrapper;
import com.easybuilder.common.utils.PageUtil;
import com.easybuilder.core.rbac.dao.SysRoleDao;
import com.easybuilder.core.rbac.entity.SysRole;
import com.easybuilder.core.rbac.service.SysRoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleDao roleDao;

    /** 根据userId，查询角色
     * @param userId*/
    @Override
    public List<SysRole> getByUserId(Long userId) {

        return roleDao.getByUserId(userId);
    }

    @Override
    public void save(SysRole sysRole) {
        if (sysRole.getId() == null) {
            sysRole.setCreateTime(new Date());
            roleDao.insertSelective(sysRole);
        } else {
            sysRole.setUpdateTime(new Date());
            roleDao.updateByPrimaryKeySelective(sysRole);
        }
    }

    @Override
    public PageResultWrapper<SysRole> page(PageQueryWrapper<SysRole> queryWrapper) {
        Page page = PageHelper.startPage(queryWrapper.getPageNum(), queryWrapper.getPageSize());
        Example example = PageUtil.buildExample(queryWrapper.getSearchEntity(),SysRole.class);
        example.orderBy("createTime").desc();
        roleDao.selectByExample(example);
        return PageUtil.build(page);
    }
}
